package com.xbongbong.paas.controller;/*/*/


import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageSetDetailDTO;
import com.xbongbong.pro.message.pojo.dto.MessageSetEnableDTO;
import com.xbongbong.pro.message.pojo.dto.MessageSetMenuListDTO;
import com.xbongbong.pro.message.pojo.dto.MessageSetSaveDTO;
import com.xbongbong.pro.message.pojo.vo.MessageSetDetailVO;
import com.xbongbong.pro.message.pojo.vo.MessageSetEnableVO;
import com.xbongbong.pro.message.pojo.vo.MessageSetMenuListVO;
import com.xbongbong.pro.message.pojo.vo.MessageSetSaveVO;
import com.xbongbong.pro.message.service.ParentMessageSetService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * Description: 消息设置controller
 * @author 魏荣杰
 * @date 2019/1/4 10:04
 * @version v1.0
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/message/set")
public class MessageSetController {

    @Resource
    private ParentMessageSetService parentMessageSetService;

    @RequestMapping(value = "/menuList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String menuList(@RequestBody @Valid MessageSetMenuListDTO messageSetMenuListDTO, BindingResult br) {
        XbbResponse<MessageSetMenuListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageSetMenuListVO messageSetMenuListVO = parentMessageSetService.getMenuList(messageSetMenuListDTO);
                response = new XbbResponse<>(messageSetMenuListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/detailList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detailList(@RequestBody @Valid MessageSetDetailDTO messageSetDetailDTO, BindingResult br) {
        XbbResponse<MessageSetDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageSetDetailVO messageSetDetailVO = parentMessageSetService.getSetDetailList(messageSetDetailDTO);
                response = new XbbResponse<>(messageSetDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid MessageSetSaveDTO messageSetSaveDTO, BindingResult br) {
        XbbResponse<MessageSetSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageSetSaveVO messageSetDetailVO = parentMessageSetService.saveMessageSet(messageSetSaveDTO);
                response = new XbbResponse<>(messageSetDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid MessageSetEnableDTO messageSetEnableDTO, BindingResult br) {
        XbbResponse<MessageSetEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageSetEnableVO messageSetEnableVO = parentMessageSetService.enableMessageSet(messageSetEnableDTO);
                response = new XbbResponse<>(messageSetEnableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
